home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’92 / not! / noway.c next >
Encoding:
C/C++ Source or Header  |  1992-05-21  |  4.2 KB  |  194 lines  |  [TEXT/KAHL]

  1. /* noway.c -
  2. /*     replace OK and Cancel in buttons with Way/NoWay or Not */
  3. /*  play appropriate sound if button clicked */
  4. /*  sounds are marked as 'sysheap/purgeable', */
  5. /*      snd 128-137 == "positive sounds" */
  6. /*       snd 138-147 == "negative sounds" */
  7.  
  8. /* © copyright 1992, Eric Slosser, all rights reserved */
  9.  
  10. #include <SetupA4.h>
  11. #include <Sound.h>
  12. #include <Traps.h>
  13.  
  14. typedef pascal short (*TrackType) (ControlHandle,Point,ProcPtr);
  15.  
  16. long        gOldNewControl;
  17. TrackType    gOldTrackControl;
  18. long        gOldSetCTitle;
  19.  
  20. WindowPtr    gLastWindow;    /* for "noway/way" combinations */
  21. long        gDirID;
  22. short        gVRefNum;
  23. Str31        gName;
  24.  
  25. Str15    kWay    =    "\pWay",
  26.         kNoWay    =     "\pNo Way",
  27.         kNot    =    "\pNot!",
  28.         kOkay    =    "\pOK",
  29.         kCancel    =    "\pCancel";
  30.  
  31. /*------------------------------*/
  32. pascal ControlHandle myNewControl( WindowPtr wind, Rect bounds, Str255 title, 
  33.     Boolean visible, short val, short min, short max, short procID, long refCon);
  34. pascal void  mySetCTitle( ControlHandle ctl, Str255 title );
  35. pascal short myTrackControl( ControlHandle ctl, Point p, ProcPtr action );
  36. int             pstrcmp ( StringPtr s, StringPtr t );
  37.  
  38. /*------------------------------*/
  39. void    ChangeTitle( StringPtr *title, WindowPtr wind );
  40. void    ChangeTitle( StringPtr *title, WindowPtr wind )
  41. {
  42.     if ( !pstrcmp(*title,kOkay) ) {
  43.         *title = kWay;
  44.         gLastWindow = wind;
  45.         }
  46.     else if ( !pstrcmp(*title,kCancel) ) {
  47.         *title = (gLastWindow == wind) ? kNoWay    : kNot;
  48.         }
  49. }
  50. /*------------------------------*/
  51. pascal ControlHandle myNewControl(
  52.     WindowPtr    wind,
  53.     Rect        bounds,
  54.     Str255        title,
  55.     Boolean        visible,
  56.     short val, short min, short max,
  57.     short        procID,
  58.     long        refCon)
  59. {
  60.     SetUpA4();
  61.     if ( procID==pushButProc )
  62.         ChangeTitle( &title, wind );
  63.     asm { 
  64.         move.l     gOldNewControl,a0
  65.         move.l    (sp)+,a4    /* RestoreA4 */
  66.         unlk    a6
  67.         jmp        (a0)
  68.         }
  69. }    /* myNewControl */
  70. /*------------------------------*/
  71. pascal void mySetCTitle(
  72.     ControlHandle    ctl,
  73.     Str255            title)
  74. {
  75.     SetUpA4();
  76.     ChangeTitle( &title, (**ctl).contrlOwner );
  77.     asm { 
  78.         move.l     gOldSetCTitle,a0
  79.         move.l    (sp)+,a4    /* RestoreA4 */
  80.         unlk    a6
  81.         jmp        (a0)
  82.         }
  83. }
  84. /*------------------------------*/
  85. void    PlaySound( Boolean postiveSound );
  86. void    PlaySound( Boolean postiveSound )
  87. {
  88.     short    saved,id,resRef;
  89.     Handle    soundH;
  90.     
  91.     saved = CurResFile();
  92.     resRef = HOpenResFile( gVRefNum, gDirID, gName, fsRdWrPerm);
  93.     if ( resRef<0 ) 
  94.         goto error;
  95.  
  96.     UseResFile(resRef);
  97.     do {
  98.         id = Random();
  99.         if ( id<0 ) id = -id;
  100.         id = id%10;
  101.         id += postiveSound? 128 : 138;
  102.         soundH = GetResource('snd ',id);
  103.         }
  104.     while (soundH==nil);
  105.     
  106.     SndPlay(nil,soundH,true);
  107.     
  108.     UseResFile(saved);
  109. error:
  110.     ;
  111. }
  112. /*------------------------------*/
  113. pascal short myTrackControl( 
  114.     ControlHandle    ctl,
  115.     Point            p,
  116.     ProcPtr            action )
  117. {
  118.     short    part;
  119.     unsigned char *cTitle;
  120.     
  121.     SetUpA4();
  122.     part = (*gOldTrackControl)( ctl,p,action ); /* can you say "tail patch"? */
  123.     if ( part==inButton ) {
  124.         cTitle = (**ctl).contrlTitle;
  125.         if (!pstrcmp(cTitle,kNoWay) || !pstrcmp(cTitle,kNot) )
  126.             PlaySound(false);
  127.         else if (!pstrcmp(cTitle,kWay) )
  128.             PlaySound(true);
  129.         }
  130.     RestoreA4();
  131.     return(part);
  132. }    /* myTrackControl */
  133. /*------------------------------*/
  134. void    SaveResFileInfo(void);
  135. void    SaveResFileInfo(void)
  136. {
  137.     Handle    h;
  138.     short    resRef;
  139.     FCBPBRec pb;
  140.     
  141.     asm { 
  142.         RecoverHandle
  143.         move.l    a0,h
  144.         }
  145.  
  146.     resRef = HomeResFile(h);
  147.     DetachResource(h);
  148.     
  149.     pb.ioFCBIndx = 0;
  150.     pb.ioFCBIndx = 0;
  151.     pb.ioNamePtr = gName;
  152.     pb.ioRefNum = resRef;
  153.     PBGetFCBInfo(&pb,false);
  154.     
  155.     gVRefNum = pb.ioFCBVRefNum;
  156.     gDirID = pb.ioFCBParID;
  157. }
  158. /*------------------------------*/
  159. int        pstrcmp ( StringPtr s, StringPtr t )
  160. {
  161.     register int    i;
  162.     
  163.     if ( *s != *t )
  164.         return ( *s - *t );    /* >0 if s is longer, <0 is t is longer */
  165.         
  166.     for ( i=*s; *s==*t && i>=0; s++,t++,i-- )
  167.         ;
  168.     
  169.     if ( i<0 ) 
  170.         return( 0 );
  171.     else
  172.         return ( *s - *t );
  173. }
  174. /*------------------------------*/
  175. main()
  176. {
  177.     RememberA0();
  178.     SetUpA4();
  179.     
  180.     SaveResFileInfo();
  181.     
  182.     gOldTrackControl = (TrackType) NGetTrapAddress(_TrackControl,ToolTrap);
  183.     NSetTrapAddress( (long) myTrackControl,_TrackControl,ToolTrap);
  184.  
  185.     gOldNewControl = NGetTrapAddress( _NewControl,ToolTrap);
  186.     NSetTrapAddress( (long) myNewControl,_NewControl,ToolTrap);
  187.  
  188.     gOldSetCTitle = NGetTrapAddress( _SetCTitle,ToolTrap);
  189.     NSetTrapAddress( (long) mySetCTitle,_SetCTitle,ToolTrap);
  190.  
  191.     RestoreA4();
  192. }
  193. /*------------------------------*/
  194.